package nobodyCodesThat;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;

public class AnimationThread extends Thread {
	private static Singleton instance = Singleton.getInstance();

	public void run() {
		while (instance.running) {
			long currentTime = System.currentTimeMillis();
			int delayTime = instance.loader.data[instance.animImageNumber].delayTime;
			while (currentTime + delayTime * 10 > System.currentTimeMillis()) {
				// Wait till the delay time has passed
			}
			if (!instance.display.isDisposed()) {
				instance.display.asyncExec(new Runnable() {
					public void run() {
						// Increase the variable holding the frame number
						instance.animImageNumber = instance.animImageNumber == instance.loader.data.length - 1 ? 0
								: instance.animImageNumber + 1;
						// Draw the new data onto the image
						ImageData nextFrameData = instance.loader.data[instance.animImageNumber];
						Image frameImage = new Image(instance.display,
								nextFrameData);
						instance.gc.drawImage(frameImage, nextFrameData.x,
								nextFrameData.y);
						frameImage.dispose();
						instance.canvasAnimGif.redraw();
					}
				});
			}
		}
	}
}
